STD_OUTPUT EQU -11
STD_INPUT  EQU -10

GetStdHandle PROTO NEAR32 stdcall,
    nStdHandle:DWORD

ReadFile PROTO NEAR32 stdcall,
    hFile:DWORD, lpBuffer:NEAR32, nNumberOfCharsToRead:DWORD,
    lpNumberOfBytesRead:NEAR32, lpOverlapped:NEAR32

WriteFile PROTO NEAR32 stdcall,
    hFile:DWORD, lpBuffer:NEAR32, nNumberOfCharsToWrite:DWORD,
    lpNumberOfBytesWritten:NEAR32, lpOverlapped:NEAR32

.DATA

written    DWORD  ?
read       DWORD  ?
strAddr    DWORD  ?
strLength  DWORD  ?
hStdOut    DWORD  ?
hStdIn     DWORD  ?

.CODE

; outproc(source)
; Procedure to display null-terminated string
; No registers are changed; flags are not affected.

outproc     PROC   NEAR32
            push   ebp                 ; save base pointer
            mov    ebp, esp            ; establish stack frame
            pushad
            pushfd                     ; save flags

            mov    esi,[ebp+8]         ; source address
            mov    strAddr, esi

; find string length
            mov    strLength, 0        ; initialize string length
WhileChar:  cmp    BYTE PTR [esi], 0   ; character = null?
            jz     EndWhileChar        ; exit if so
            inc    strLength           ; increment character count
            inc    esi                 ; point at next character
            jmp    WhileChar
EndWhileChar:

            INVOKE GetStdHandle,       ; get handle for console output
              STD_OUTPUT
            mov    hStdOut, eax

            INVOKE WriteFile,
              hStdOut,                 ; file handle for screen
              strAddr,                 ; address of string
              strLength,               ; length of string
              NEAR32 PTR written,      ; bytes written
              0                        ; overlapped mode

            popfd                      ; restore flags
            popad                      ; restore registers
            pop    ebp
            ret    4                   ;exit, discarding parameter
outproc     ENDP

; inproc(dest,length)
; Procedure to input a string from keyboard.
; The string will be stored at the address given by dest.
; The length parameter gives the size of the user's buffer.  It is assumed 
; that there will be room for the string and a null byte.
; The string will be terminated by a null character (00h).
; Flags are unchanged.

inproc      PROC   NEAR32
            push   ebp                 ; save base pointer
            mov    ebp, esp            ; establish stack frame
            pushad                     ; save all registers
            pushfd                     ; save flags

            INVOKE GetStdHandle,       ; get handle for console
              STD_INPUT
            mov    hStdIn, eax

            mov    ecx, [ebp+8]        ; string length
            mov    strLength, ecx

            mov    esi, [ebp+12]       ; source address
            mov    strAddr, esi

            INVOKE ReadFile,
              hStdIn,                  ; file handle for keyboard
              strAddr,                  ; address of string
              strLength,                ; length of string
              NEAR32 PTR read,          ; bytes read
              0                         ; overlapped mode

            mov    ecx, read           ; number of bytes read
            mov    BYTE PTR [esi+ecx-2],0 ; replace CR/LF by trailing null

            popfd                      ; restore flags
            popad                      ; restore registers
            pop    ebp 
            ret    8                   ; exit, discarding parameters
inproc      ENDP

